<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Camera extends Controller_Index_Base
{
    public function action_index()
    {          
        $cameras = ORM::factory('model')->get_cameras_for_main();
        $compare_data = array();
        foreach (ORM::factory('specs_camera')->get_sorted_camera_types() as $value) {
            $type = $value['id'];
            foreach ($cameras[$type] as $data) {
                $camera = $data['camera'];
                $specs  = $data['specs']; 
                $compare_data[$camera->id] = ORM::factory('specs_camera')->compare_with_same_type($camera, $specs, 3);   
            }
        }   
        
        $content               = new View('index/category/camera/index_tpl'); 
        $content->cameras      = $cameras;      
        $content->compare_data = $compare_data;
        $content->available_specs = ORM::factory('specs_camera')->get_available_specs(true); 
        $this->print_template('Сравнение и подбор фотоаппаратов и фотокамер', $content);
    }
    
    public function action_type()
    {
        $type = $this->request->param('type') ? $this->request->param('type') : null;
        $page = $this->request->param('page') ? $this->request->param('page') : 1;
        
        $cameras = ORM::factory('model')->get_cameras_by_type($type, $page);
        $compare_data = array();
        foreach ($cameras as $data) {
            $camera = $data['camera'];
            $specs  = $data['specs']; 
            $compare_data[$camera->id] = ORM::factory('specs_camera')->compare_with_same_type($camera, $specs, 3);   
        }   
        
        $stat                  = ORM::factory('specs_camera')->get_stat();
        $content               = new View('index/category/camera/type_tpl'); 
        $content->cameras      = $cameras;      
        $content->compare_data = $compare_data;
        $content->available_specs = ORM::factory('specs_camera')->get_available_specs(true); 
        $content->type         = $type;
        $content->pagination   = array(
            'page'     => $page,
            'per_page' => Model_Model::get_models_per_page(),
            'count'    => $stat[$type],
        );
        $this->print_template(Model_Main::ucfirst_utf8(Model_Specs_Camera::$_extended_camera_types[$type]['forms']['many']), $content);
    }
    
    public function action_compare()
    {    
        if (HTTP_Request::POST == $this->request->method()) {
            $model1 = $this->request->post('model-1');
            $model2 = $this->request->post('model-2');
            $model1 = ORM::factory('model', $model1); 
            $model2 = ORM::factory('model', $model2); 
            Request::current()->redirect(Helper_Index::compare_url($model1, $model2));                 
        }
        $content = new View('index/category/camera/compare_tpl'); 
        $content->cameras = ORM::factory('model')->get_cameras_for_compare();  
        $this->print_template('Выбор фотоаппаратов для сравнения', $content);
    }
} 
